package com.example.lib_net

import com.example.lib_net.api.Api
import com.example.lib_net.api.MallApi
import com.example.lib_net.constant.API
import com.example.lib_net.constant.API_MALL
import com.example.lib_net.interceptor.HttpHeaderInterceptor
import com.example.lib_net.interceptor.HttpMallHeaderInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.net.ProxySelector
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier
import javax.inject.Singleton

/**
 * @author yaotianxue
 * @description
 * @date 2025/1/14
 */
@Module
@InstallIn(SingletonComponent::class)
class NetModule {
    @Provides
    @Singleton
    @CommonOkhttp
    fun provideOkhttpClient(): OkHttpClient {
        //判断当前是否处于debug环境
        val httpLoggingInterceptor = HttpLoggingInterceptor()
//        if (BuildConfig.DEBUG){
            httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
//        }
        return OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(HttpHeaderInterceptor())
            .addInterceptor(httpLoggingInterceptor)
            //防止抓包
            .proxySelector(ProxySelector.getDefault())
            //ssl认证
            .sslSocketFactory(  SSLSocketFactoryUtil.createSSLSocketFactory(),
                SSLSocketFactoryUtil.createTrustAllManager())
            .hostnameVerifier(SSLSocketFactoryUtil.TrustAllHostnameVerifier())
            .build()

    }
    @Provides
    @Singleton
    @MallOkhttp
    fun provideMallOkhttpClient(): OkHttpClient {
        //判断当前是否处于debug环境
        val httpLoggingInterceptor = HttpLoggingInterceptor()
//        if (BuildConfig.DEBUG){
           httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
//        }
        return OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(HttpMallHeaderInterceptor())
            .addInterceptor(httpLoggingInterceptor)
            //防止抓包
            .proxySelector(ProxySelector.getDefault())
            //ssl认证
            .sslSocketFactory(  SSLSocketFactoryUtil.createSSLSocketFactory(),
                SSLSocketFactoryUtil.createTrustAllManager())
            .hostnameVerifier(SSLSocketFactoryUtil.TrustAllHostnameVerifier())
            .build()

    }

    @Provides
    @Singleton
    @CommonRetrofit
    fun provideRetrofit(@CommonOkhttp okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(API.BASE_URL) //动态配置baseurl
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @Singleton
    @MallRetrofit
    fun provideMallRetrofit(@MallOkhttp okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(API_MALL.BASE_MALL_URL)//动态配置baseurl
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @Singleton
    fun provideApi(@CommonRetrofit retrofit: Retrofit): Api {
        return retrofit.create(Api::class.java)
    }

    @Provides
    @Singleton
    fun provideMallApi(@MallRetrofit retrofit: Retrofit): MallApi {
        return retrofit.create(MallApi::class.java)
    }

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonRetrofit

    //对接数引网Retrofit
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class MallRetrofit



    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonOkhttp

    //对接数引网okhttp
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class MallOkhttp


}